//
//  CapabilityAssessmentViewController.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/1/11.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "CapabilityAssessmentViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"
#import "CapabilityAssessmentView.h"
#import "CardModel.h"
#import "JiGuangUnit.h"

#import "PaperViewControllerBuild.h"

@interface CapabilityAssessmentViewController () <capabilityAssessmentDelegate>

@property (strong, nonatomic) CapabilityAssessmentView *capabilityAssessmentView;
@property (weak, nonatomic) IBOutlet UIButton *allAnaly;
@property (weak, nonatomic) IBOutlet UIButton *errAnaly;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;

@end

@implementation CapabilityAssessmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupConfigureView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidAppear:(BOOL)animated{
    NSUInteger type = [self.ques_type integerValue];
    if (type == 8) {
        _allAnaly.hidden = YES;
        _errAnaly.hidden = YES;
    }
}
- (void)headRightAction{


//    JSHAREMessage *message = [JSHAREMessage message];
//    message.mediaType = JSHARELink;
//    message.url =[[OtherNetworking sharedManager] kShareUrlCourseFace:(self.model.report_id)];
//    message.text = self.model.course_des;
//    message.title = self.model.course_name;
//    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.argumentModel.course_image]];
//    message.image = imageData;
    NSDictionary *dic = @{@"share_type":@"8",@"share_id":self.model.report_id};
//    [[JiGuangUnit sharedInstant] shareViewAddSubview:self.view withMessage:message andDic:dic];

}
- (void)setupConfigureView {
    [self loadHeadView:@"答题报告" addRightBtn:YES rightImageName:@"CourseDetailShareIcon" rightTitle:nil];
    if (kDevice_Is_iPhoneX) {
        self.bottomConstraint.constant = kBottomSafeHeight;
    }
    
    self.fd_interactivePopDisabled = YES;       // 禁止滑动返回
    self.allAnaly.backgroundColor = MainThemeColor;
    self.errAnaly.backgroundColor = MainThemeColor;

    self.capabilityAssessmentView = [[CapabilityAssessmentView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kNavigationBarHeight - kBottomSafeHeight - 46)];
    self.capabilityAssessmentView.didDelegate = self;
    [self.view addSubview:self.capabilityAssessmentView];

    if (![self.backViewController isEqualToString:@"PracticeRecordViewController"]) {
        // 答题记录，加载过来的，不需要刷新数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateUserAnswerNotification" object:nil];
    }
    
    if (self.model) {       // 直接展示
        [self.capabilityAssessmentView setDataModel:self.model];
    } else {                // 加载数据展示
        self.capabilityAssessmentView.hidden = YES;
        self.viewModel = [[BaseViewModel alloc] initWithUrl:@"Question/znbg"];
        self.viewModel.params.body = @{@"report_id" : self.report_id ? : @""};
        @weakify(self)
        [self didViewModelWithCompletionBlock:^(id receive) {
            @strongify(self)
            if ([receive isKindOfClass:[CapabilityAssessmentModel class]]) {
                self.model = receive;
                [self.capabilityAssessmentView setDataModel:self.model];
                self.capabilityAssessmentView.hidden = NO;
            }
        }];
        [self.viewModel.command execute:@"CapabilityAssessmentModel"];
    }
}

- (void)headLeftAction {
    if (self.backViewController == nil) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        __block BOOL isexist = NO;
        NSArray *vcs = self.navigationController.viewControllers;
        [vcs enumerateObjectsUsingBlock:^(UIViewController *obj, NSUInteger idx, BOOL *stop) {
            if ([obj isKindOfClass:NSClassFromString(self.backViewController)]) {
                isexist = YES;
                [self.navigationController popToViewController:obj animated:YES];
                *stop = YES;
            }
        }];
        
        if (isexist == NO) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    }
}

#pragma mark - capabilityAssessmentDelegate
// 点击单个解析
- (void)didSelectAtIndexPath:(NSIndexPath *)indexPath {
    PaperViewController *viewController;
    
    CardModel *cardModel = self.model.user_answer_report[indexPath.row];
    NSDictionary *dic = @{@"report_id" : self.model.report_id ? : @"", @"ques_id" : cardModel.ques_id ? : @""};
    NSUInteger type = [self.ques_type integerValue];
    if (type == 0) {            // 智能刷题
        viewController = [PaperViewControllerBuild createIntelligentSignalAnalyViewControllerWithData:dic];
    } else if(type == 2) {      // 高频错题
        viewController = [PaperViewControllerBuild createHighFrequencySignalAnalyViewControllerWithData:dic];
    } else if (type == 1) {     // 考点专练
        viewController = [PaperViewControllerBuild createExerciseSignalAnalyViewControllerWithData:dic];
        
    }
        //以下暂未定义
//    } else if (type == 6) {     // 每日一练
//        viewController = [PaperViewControllerBuild createPracticeDailySignalAnalyViewControllerWithData:dic];
//    } else if (type == 7) {     // 错题练习
//        viewController = [PaperViewControllerBuild createWrongRecordSignalAnalyViewControllerWithData:dic];
//    }
    
    if (viewController) {
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

#pragma mark - btn action

// 点击所有解析
- (IBAction)didAllAnalyAction:(id)sender {
    PaperViewController *viewController;
    
    NSDictionary *dic = @{@"report_id" : self.model.report_id ? : @""};
    NSUInteger type = [self.ques_type integerValue];
    if (type == 0) {            // 智能刷题
        viewController = [PaperViewControllerBuild createIntelligentAllAnalyViewControllerWithData:dic];
    } else if(type == 2) {      // 高频错题
        viewController = [PaperViewControllerBuild createHighFrequencyAllAnalyViewControllerWithData:dic];
    } else if (type == 1) {     // 考点专练
        viewController = [PaperViewControllerBuild createExerciseAllAnalyViewControllerWithData:dic];
        
    }
//    } else if (type == 6) {     // 每日一练
//        viewController = [PaperViewControllerBuild createPracticeDailyAllAnalyViewControllerWithData:dic];
//    } else if (type == 7) {     // 错题练习
//        viewController = [PaperViewControllerBuild createWrongRecordAllAnalyViewControllerWithData:dic];
//    }
    
    if (viewController) {
        [self.navigationController pushViewController:viewController animated:YES];
    }
}

// 点击错误解析
- (IBAction)didWrongAnalyAction:(id)sender {
    PaperViewController *viewController;
    
    NSDictionary *dic = @{@"report_id" : self.model.report_id ? : @""};
    NSUInteger type = [self.ques_type integerValue];
    if (type == 0) {            // 智能刷题
        viewController = [PaperViewControllerBuild createIntelligentWrongAnalyViewControllerWithData:dic];
    } else if(type == 2) {      // 高频错题
        viewController = [PaperViewControllerBuild createHighFrequencyWrongAnalyViewControllerWithData:dic];
    } else if (type == 1) {     // 考点专练
        viewController = [PaperViewControllerBuild createExerciseWrongAnalyViewControllerWithData:dic];
    }
        
        
//    } else if (type == 6) {     // 每日一练
//        viewController = [PaperViewControllerBuild createPracticeDailyWrongAnalyViewControllerWithData:dic];
//    } else if (type == 7) {     // 错题练习
//        viewController = [PaperViewControllerBuild createWrongRecordWrongAnalyViewControllerWithData:dic];
//    }
    
    if (viewController) {
        [self.navigationController pushViewController:viewController animated:YES];
    }
}



@end
